package net.wnb.mineclearance.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import net.wnb.mineclearance.helper.GameManager;
import net.wnb.mineclearance.model.Game;
import net.wnb.mineclearance.model.RunningTimeRefreshedListener;

public class GamePane extends JPanel implements ActionListener, RunningTimeRefreshedListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final int DEFAULT_DELAY = 50;
	private static final int GRID_SIZE = 40;
	private static final int MARGIN_HORIZONTAL = 0;
	private static final int MARGIN_VERTICAL = 80;
	private Game game;
	private JButton btnBack;
	private JButton btnRestart;
	private JLabel labelTime;
	private GameView view;
	
	public GamePane(Game game){
		this.game = game;
		init();
		
		JPanel toolPane = new JPanel();
		toolPane.setLayout(new BoxLayout(toolPane, BoxLayout.X_AXIS));
		toolPane.add(Box.createRigidArea(new Dimension(5, 50)));
		toolPane.add(btnBack);
		toolPane.add(Box.createRigidArea(new Dimension(5, 50)));
		toolPane.add(btnRestart);
		toolPane.add(Box.createGlue());
		toolPane.add(labelTime);
		toolPane.add(Box.createRigidArea(new Dimension(5, 50)));
		
		setLayout(new BorderLayout());
		add(toolPane, BorderLayout.NORTH);
		add(view, BorderLayout.CENTER);
		
		setSize(GRID_SIZE * game.getColCount() + MARGIN_HORIZONTAL, GRID_SIZE * game.getRowCount() + MARGIN_VERTICAL);
		view.start();
	}

	private void init() {
		btnBack = new JButton("返回");
		btnRestart = new JButton("重新开始");
		labelTime = new JLabel("00:00:00");
		view = new GameView(new GameAnimation(game), DEFAULT_DELAY, true);
		
		btnBack.setMaximumSize(new Dimension(100, 32));
		btnRestart.setMaximumSize(new Dimension(100, 32));
		labelTime.setMaximumSize(new Dimension(100, 32));
		view.setMaximumSize(new Dimension(GRID_SIZE * game.getColCount(), GRID_SIZE * game.getRowCount()));
		view.setSize(new Dimension(GRID_SIZE * game.getColCount(), GRID_SIZE * game.getRowCount()));
		
		btnBack.addActionListener(this);
		btnRestart.addActionListener(this);
		game.addRunningTimeRefreshedListener(this);
	}

	@Override
	public void actionPerformed(ActionEvent event) 	{
		Object source = event.getSource();
		if(source.equals(btnBack)){
			if(game.getState() == Game.STATE_PROCESSING){
				GameManager.write(game);
			}else{
				GameManager.delete();
			}
			
			MainWindow win = MainWindow.getInstance();
			win.setPane(new SelectionPane());
		}else if(source.equals(btnRestart)){
			view.stop();
			remove(view);
			
			this.game.dispose();
			this.game = new Game(game.getRowCount(), game.getColCount(), game.getMineCount());
			view = new GameView(new GameAnimation(this.game), DEFAULT_DELAY, true);
			add(view, BorderLayout.CENTER);
			game.start();
			view.start();
			
			game.addRunningTimeRefreshedListener(this);
			validate();
		}
	}

	@Override
	public void onRunningTimeRefreshed(int runningTime) {
		int hour = runningTime / (60 * 60);
		int minute = (runningTime - hour * 60 * 60) / 60;
		int second = runningTime - hour * 60 * 60 - minute * 60;
		StringBuilder builder = new StringBuilder();
		builder.append(hour);
		builder.append(":");
		if(minute < 10)
			builder.append("0");
		builder.append(minute);
		builder.append(":");
		if(second < 10)
			builder.append("0");
		builder.append(second);
		
		labelTime.setText(builder.toString());
	}
}
